<template>
  <basePage :show-tabbar="false" :page-params="pageParams" @testAgainFn="loadOrderFn">
    <orderConfirm
      v-if="settleType == 1201"
      ref="orderConfirm"
      :show-warehouse-label="showWarehouseLabel"
      :order-detail-data="orderDetailData"
      :order-product-list="orderProductList"
      :total-record="totalRecord"
      @refreshPage="loadOrderFn"
    />
    <childPayment
      v-else
      :show-warehouse-label="showWarehouseLabel"
      :order-detail-data="orderDetailData"
      :order-product-list="orderProductList"
      :total-record="totalRecord"
    />
  </basePage>
</template>

<script>
import basePage from '@/components/base-page';
import childPayment from '@/payments/payment/childPayment'
import orderConfirm from '@/payments/orderConfirm/index'
import {
  orderGoodsItems,
  orderInfoByOrderNo,
} from '@/api/payment'
import {
  checkMemeberStatusFn
} from '@/common/common';
export default {
  name: 'Payment',
  components: {
    basePage,
    childPayment,
    orderConfirm
  },
  data() {
    return {
      orderDetailData: {},
      orderProductList: [],
      totalRecord: 0,
      orderNo: '',
      settleType: '',
      showWarehouseLabel: false, //是否显示仓位标签
      pageParams: {
        state: null,
        emptyTitle: '',
        errorTitle: '',
        showButton: true
      },
    }
  },
  onLoad(option) {
    if (option.pageSource && option.pageSource == 'local') {
      this.orderNo = option.orderNo
    } else {
      // #ifdef MP-ALIPAY
      this.orderNo = this.vuex_qrOrderNo
      // #endif
      // #ifdef MP-WEIXIN
      // 获取到二维码原始链接内容
      const src = decodeURIComponent(option.q)
      const id = src.match(/id=(\S*)/)[1]
      this.orderNo = id;
      // #endif
    }
    // this.loadOrderFn()
  },
  onPullDownRefresh() {
    //刷新后的效果
    this.loadOrderFn()
  },
  onReady() {
    this.loadOrderFn()
  },
  methods: {
    /*未登录跳转登录*/
    returnLoginFn(type, orderNo) {
      const branchData = JSON.stringify({
        branchId: this.orderDetailData.branchId,
        branchName: this.orderDetailData.branchName,
        settleType: this.orderDetailData.settleType
      })
      const callbackUrl = `/payments/payment/payment`
      const urlParams = JSON.stringify({ orderNo: orderNo, pageSource: 'local' })
      uni.redirectTo({
        url: `/login/login/codeLogin?type=${type}&callbackUrl=${callbackUrl}&urlParams=${urlParams}&registParams=${branchData}`
      })
    },
    // 返回当前页调用刷新该页面的方法
    /*refreshRequest() {
      this.loadOrderFn()
    },*/
    loadOrderFn() {
      this.orderProductList = []
      this.orderDetail()
      if (this.settleType == 1201) {
        this.$refs.orderConfirm.initPageFn()
      }
      this.orderProductListFn()
    },
    async getMemeberStatusFn() {
      /*判断当前账号是否是有效账号*/
      if (this.vuex_loginStatus && this.vuex_accountInfo && this.vuex_accountInfo.loginAccount) {
        const memeberStatus = await checkMemeberStatusFn({ loginAccount: this.vuex_accountInfo.loginAccount })
        if (!memeberStatus) {
          if (this.settleType == 1201) {
            this.returnLoginFn('confirm', this.orderDetailData.orderNo)
          } else {
            this.returnLoginFn('payPage', this.orderDetailData.orderNo)
          }
          return
        }
      }
    },
    orderDetail() {
      const params = {
        'orderNo': this.orderNo,
      }
      orderInfoByOrderNo(params).then((res) => {
        if (res.errorCode != 0) {
          this.setPageParamsFn('ERROR', res.msg)
          return
        }
        this.setPageParamsFn(null)
        this.$nextTick(() => {
          this.orderDetailData = res.data
          this.settleType = res.data.settleType
          console.log(this.settleType);
          /* 仓位:配方方式为鑫方盛物流、快递、快运-送货上门、快运-站点自提、专车包车时不显示，其他自提类的显示仓位信息
          * 配送方式;10=快递；20=快运-送货上门；21=快运-站点自提；30=专车包车；40=鑫方盛物流；50=上门自提 */
          const sentType = res.data.sentType
          if (sentType != 10 && sentType != 20 && sentType != 21 && sentType != 30 && sentType != 40) {
            this.showWarehouseLabel = true
          }
          let title = '订单支付'
          if (res.data.settleType == 1201) {
            title = '订单确认'
          }
          uni.setNavigationBarTitle({
            title: title
          })
          this.$forceUpdate()
        })
        this.getMemeberStatusFn()
      }).catch((res) => {
        this.setPageParamsFn('ERROR', res.msg)
      })
    },
    orderProductListFn() {
      const params = {
        'orderNo': this.orderNo,
        'pageNum': 1,
        'pageSize': 3
      }
      orderGoodsItems(params).then((res) => {
        uni.stopPullDownRefresh() //刷新数据之后停止刷新效果
        if (res.errorCode != 0) {
          this.setPageParamsFn('ERROR', res.msg)
          return
        }
        this.setPageParamsFn(null)
        this.$nextTick(() => {
          res.data.result.map((item) => {
            item.oldBuyyerCount = item.buyyerCount
            return item
          })
          this.orderProductList = res.data.result
          this.totalRecord = res.data.totalRecord
          this.$forceUpdate()
        })
      }).catch((res) => {
        this.setPageParamsFn('ERROR', res.msg)
        uni.stopPullDownRefresh() //刷新数据之后停止刷新效果
      })
    },
    setPageParamsFn(pageType = null, title = '', showButton = true) {
      this.$nextTick(() => {
        this.pageParams.state = pageType
        this.pageParams.emptyTitle = title
        this.pageParams.errorTitle = title
        this.pageParams.showButton = showButton
      })
    }
  }
}
</script>

<style scoped lang="scss">
.order-payment-con{
  padding-top:10rpx;
  padding-bottom:170rpx;
  .sticky-btn{
    float: right;
    margin:8rpx 0 0 58rpx;
  }
}
</style>
